package com.app.ui.popupview;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.wxj.zx.R;


/**
 * 自定义的弹窗
 * Created by Administrator on 2016/9/14.
 */
public abstract class CustomPopupWindow extends PopupWindow implements PopupWindow.OnDismissListener {
    private Activity activity;
    private View contextView;

    public CustomPopupWindow(Activity activity) {
        this.activity = activity;
        onCreate();
    }

    protected abstract void onCreate();

    protected void setPopupWindowView(int layoutId) {
        LayoutInflater inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(layoutId, null);
        setView(view);
    }

    protected void setPopupWindowView(View view) {
        setView(view);
    }

    private void setView(View view) {
        contextView = view;
        setContentView(view);
        setLayoutParams();
        setaAnimation();
        //点击空白处时，隐藏掉pop窗口
        setFocusable(true);
        ColorDrawable dw = new ColorDrawable(0x00);
        setBackgroundDrawable(dw);
        setOnDismissListener(this);
    }

    protected void setLayoutParams() {
        setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
    }

    //设置动画
    protected void setaAnimation() {
        // 设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.PopupAnimation);
    }

    protected View findViewById(int viewId) {
        return contextView.findViewById(viewId);
    }


    /**
     * @param view 显示在这个view下
     */
    public void showDown(View view) {
        showAsDropDown(view);
        backgroundAlpha(activity, 0.5f);
    }

    /**
     * @param view     哪个view为基线
     * @param location 显示的位置（ Gravity.BOTTOM...）
     */
    public void showLocation(View view, int location) {
        showAtLocation(view, location, 0, 0);
        backgroundAlpha(activity, 0.5f);
    }

    /**
     * @param location 显示的位置（ Gravity.BOTTOM...）
     */
    public void showLocation(int location) {
        View view = activity.findViewById(android.R.id.content);
        showLocation(view, location);
    }

    private void backgroundAlpha(Activity activity, float bgAlpha) {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        activity.getWindow().setAttributes(lp);
    }

    @Override
    public void showAsDropDown(View anchor) {
        //在Android 7.0上PopupWindow.showAsDropDown不起作用的解决方法
        if (Build.VERSION.SDK_INT == 24) {
            Rect rect = new Rect();
            anchor.getGlobalVisibleRect(rect);
            int h = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom;
            setHeight(h);
        }
        super.showAsDropDown(anchor);
    }

    @Override
    public void onDismiss() {
        backgroundAlpha(activity, 1f);
    }
}
